//
//  SecondViewController.m
//  Interface
//
//  Created by Letme on 15-4-15.
//  Copyright (c) 2015年 zhenjiea. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //NSLog(@"%@", self.url1);
    //NSLog(@"array:%@", self.array);
    
    
    
    self.rootScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 100)];
    self.rootScroll.contentSize = CGSizeMake(self.cityArray.count * self.view.frame.size.width, 0);
    [self.view addSubview:self.rootScroll];
    self.rootScroll.pagingEnabled = YES;
    self.rootScroll.bounces = NO;
    self.rootScroll.delegate = self;

#warning int->long
#warning tag
        //for (long i = (self.cityArray.count - 1); i >= 0; i--) {
    for (int i = 0; i < self.cityArray.count; i++) {
            HotsiteListTableViewController *myViewController = [[HotsiteListTableViewController alloc] init];
            
        myViewController.cityUrl = [self.urlArray objectAtIndex:i];
            
        myViewController.view.frame = CGRectMake(self.view.frame.size.width * i, self.view.frame.size.height / 15 + 15, self.view.frame.size.width, self.view.frame.size.height - 100);
        
            [self addChildViewController:myViewController];
            //[self.view addSubview:myViewController.view];
            [self.rootScroll addSubview:myViewController.view];

            //myViewController.view.tag = 10000 + i;
            [self.vcArray addObject:myViewController];
        }
    
    
    
    
    //    self.myScrollView.pagingEnabled = YES;
//    self.myScrollView.bounces = NO;
    
    //[self.allModelArray addObject:model];
    
    //self.bigArray = [NSMutableArray array];
    
//    for (int i = 0; i < self.cityArray.count; i++) {
//        
//        
//        AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
//        //循环 请求  不同city的 景点列表信息
//        //NSString *url_string = self.cityUrl;
//        NSString *url_string = [self.urlArray objectAtIndex:i];
//        NSLog(@"%@", url_string);
//        //    NSString *url_string = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/3/24059/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.88265156297574&longitude=121.53968615009308"];
//        
//        AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
//        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
//        [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//            [netWorkManager stopMonitoring];
//            
//            //NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
//            
//            
//            
//            self.allModelArray = [NSMutableArray array];
//            
//            
//            
//            
//            NSDictionary *dic = responseObject;
//            NSArray *array = [dic objectForKey:@"items"];
//            
//            
//            
//            // 将一个城市所有景点 请求
//            // 强制转换
//            for (NSDictionary *smallDic in array) {
//                HotsiteListModel *model = [[HotsiteListModel alloc] init];
//                model.address = [NSString  stringWithFormat:@"%@", [smallDic objectForKey:@"address"]];
//                model.arrival_type = [NSString stringWithFormat:@"%@", [smallDic objectForKey:@"arrival_type"]];
//                model.cover_route_map_cover = [smallDic objectForKey:@"cover_route_map_cover"];
//                model.description2 = [NSString stringWithFormat:@"%@", [smallDic objectForKey:@"description"]];
//                model.fee = [NSString stringWithFormat:@"%@",[smallDic objectForKey:@"fee"]];
//                model.name = [NSString stringWithFormat:@"%@",[smallDic objectForKey:@"name"]];
//                model.name_en = [NSString stringWithFormat:@"%@", [smallDic objectForKey:@"name_en"]];
//                model.opening_time = [NSString stringWithFormat:@"%@",[smallDic objectForKey:@"opening_time"]];
//                model.recommended_reason = [NSString stringWithFormat:@"%@" ,[smallDic objectForKey:@"recommended_reason"]];
//                model.visited_count = [NSString stringWithFormat:@"%@", [smallDic objectForKey:@"visited_count"]];
//                 model.wish_to_go_count = [NSString stringWithFormat:@"%@",[smallDic objectForKey:@"wish_to_go_count"]];
//                
//                
//                model.url_id = [smallDic objectForKey:@"id"];
//                
//                [self.allModelArray addObject:model];
//                
//            }
//            [self.bigArray addObject:self.allModelArray];
//            //[self.myTabelVeiw reloadData];
//            
//#warning
//#warning  ?
//            self.myTabelVeiw = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * i, 63, self.view.frame.size.width, self.view.frame.size.height - 100)];
//            self.myTabelVeiw.delegate = self;
//            self.myTabelVeiw.dataSource = self;
//            self.myTabelVeiw.tag = 30000 + i;
//            //
//            [self.rootScroll addSubview:self.myTabelVeiw];
//            NSLog(@"创建列表");
//            [_myTabelVeiw release];
//            //[_allModelArray release];
//            
//            //    [self.myTabelVeiw reloadData];
//            
//        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//            
//            NSLog(@"失败==== %@",error);
//            
//        }];
//        
//        
//    }
    
    
    
//        self.myTabelVeiw = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * i, 63, self.view.frame.size.width, self.view.frame.size.height - 100)];
//        self.myTabelVeiw.delegate = self;
//        self.myTabelVeiw.dataSource = self;
//        //
//        [self.rootScroll addSubview:self.myTabelVeiw];
    
        
        
        
//        UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:self.cityArray];
//        segment.frame = CGRectMake(38, 10, self.view.frame.size.width - 76, self.view.frame.size.height / 15);
//        [segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
//    
//        [self.view addSubview:segment];
//        [segment release];
    self.mySegment = [[UISegmentedControl alloc] initWithItems:self.cityArray];
    self.mySegment.frame = CGRectMake(38, 10, self.view.frame.size.width - 76, self.view.frame.size.height / 15);
    [self.mySegment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.mySegment];
    self.mySegment.selectedSegmentIndex = 0;
    
    [_mySegment release];
    
}
// 拖拽开始时触发
//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView


// 拖拽加滑动整个过程触发
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"huahuahuaua");
    if (self.rootScroll.contentOffset.x / self.view.frame.size.width == 0) {
        self.mySegment.selectedSegmentIndex = 0;
    }
    if (self.rootScroll.contentOffset.x / self.view.frame.size.width == 1) {
        self.mySegment.selectedSegmentIndex = 1;
    }
    if (self.rootScroll.contentOffset.x / self.view.frame.size.width == 2) {
        self.mySegment.selectedSegmentIndex = 2;
    }
    if (self.rootScroll.contentOffset.x / self.view.frame.size.width == 3) {
        self.mySegment.selectedSegmentIndex = 3;
    }

}



- (void)segmentAction:(UISegmentedControl *)segment
{   //

    if (segment.selectedSegmentIndex == 0) {
//        UIView *temp = [self.view viewWithTag:10000 + 0];
//        [self.view bringSubviewToFront:temp];
        self.rootScroll.contentOffset = CGPointMake(self.view.frame.size.width * 0, 0);

    }
    if (segment.selectedSegmentIndex == 1) {
//        UIView *temp = [self.view viewWithTag:10000 + 1];
//        [self.view bringSubviewToFront:temp];
        self.rootScroll.contentOffset = CGPointMake(self.view.frame.size.width * 1, 0);
    }
    if (segment.selectedSegmentIndex == 2) {
//        UIView *temp = [self.view viewWithTag:10000 + 2];
//        [self.view bringSubviewToFront:temp];
        self.rootScroll.contentOffset = CGPointMake(self.view.frame.size.width * 2, 0);
    }
    if (segment.selectedSegmentIndex == 3) {
//        UIView *temp = [self.view viewWithTag:10000 + 3];
//        [self.view bringSubviewToFront:temp];
        self.rootScroll.contentOffset = CGPointMake(self.view.frame.size.width * 3, 0);
    }
    [self.view bringSubviewToFront:segment];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{   //NSLog(@".....%ld", self.allModelArray.count);
    return self.allModelArray.count ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"创建cell:%ld", indexPath.section);
    static NSString *cellIdentifier = @"cell";
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    HotsiteListModel *model = [self.allModelArray objectAtIndex:indexPath.section];
    cell.myModel = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"选择进入详情");
//    if (tableView.tag == 30000) {
//    HotsiteListModel *model = [[HotsiteListModel alloc] init];
//    NSArray *array = [self.bigArray objectAtIndex:0];
//    model = [array objectAtIndex:indexPath.section];
//    //NSLog(@"%ld", self.allModelArray.count);
//    SiteDetailPageViewController *siteDetailVC = [[SiteDetailPageViewController alloc] init];
//    siteDetailVC.model = model;
//    [self.navigationController pushViewController:siteDetailVC animated:YES];
//    }
//    if (tableView.tag == 30001) {
//        HotsiteListModel *model = [[HotsiteListModel alloc] init];
//        NSArray *array = [self.bigArray objectAtIndex:1];
//        model = [array objectAtIndex:indexPath.section];
//        //NSLog(@"%ld", self.allModelArray.count);
//        SiteDetailPageViewController *siteDetailVC = [[SiteDetailPageViewController alloc] init];
//        siteDetailVC.model = model;
//        [self.navigationController pushViewController:siteDetailVC animated:YES];
//    }
//    if (tableView.tag == 30002) {
//        HotsiteListModel *model = [[HotsiteListModel alloc] init];
//        NSArray *array = [self.bigArray objectAtIndex:2];
//        model = [array objectAtIndex:indexPath.section];
//        //NSLog(@"%ld", self.allModelArray.count);
//        SiteDetailPageViewController *siteDetailVC = [[SiteDetailPageViewController alloc] init];
//        siteDetailVC.model = model;
//        [self.navigationController pushViewController:siteDetailVC animated:YES];
//    }
//    if (tableView.tag == 30003) {
//        HotsiteListModel *model = [[HotsiteListModel alloc] init];
//        NSArray *array = [self.bigArray objectAtIndex:3];
//        model = [array objectAtIndex:indexPath.section];
//        //NSLog(@"%ld", self.allModelArray.count);
//        SiteDetailPageViewController *siteDetailVC = [[SiteDetailPageViewController alloc] init];
//        siteDetailVC.model = model;
//        [self.navigationController pushViewController:siteDetailVC animated:YES];
//    }
    
    HotsiteListModel *model = [[HotsiteListModel alloc] init];
    //NSArray *array = [self.bigArray objectAtIndex:0];
    model = [self.allModelArray objectAtIndex:indexPath.section];
    //NSLog(@"%ld", self.allModelArray.count);
    SiteDetailPageViewController *siteDetailVC = [[SiteDetailPageViewController alloc] init];
    siteDetailVC.model = model;
    [self.navigationController pushViewController:siteDetailVC animated:YES];
   
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end


